<?php
	


defined('_JEXEC') or die('Restricted access');


jimport('joomla.application.component.helper');

class NBEventMapHelperRoute
{
	
	function getArticleRoute($id, $catid = 0, $itemid = 0)
	{
		$catid_num = (int) $catid;

		
		if ($catid != 0) {
			$db         = JFactory::getDBO();
			$query = "SELECT id".
					"\n FROM #__categories".
					"\n WHERE params LIKE " .$catid_num.
					"\n AND section = 'com_nbeventmap_groupes'";
			$db->setQuery( $query );
			$list_id = $db->loadResultArray();
		}
		
		$needles = array(
			'article'  => (int) $id,
			'categorie' => $catid_num,
			'groupe' => $list_id,
		);

		
		$link = 'index.php?option=com_nbeventmap&layout=article&task=view_article&id='. $id;

		if ($itemid) {
			$link .= '&Itemid='.$itemid;
		} else {
			if($item = NBEventMapHelperRoute::_findItem($needles)) {
				$link .= '&Itemid='.$item->id;
			};
		}

		return $link;
	}

	function getCategoryRoute($catid, $display = '')
	{
		
		if ($catid != 0) {
			$db         = JFactory::getDBO();
			$query = "SELECT id".
					"\n FROM #__categories".
					"\n WHERE params LIKE " .$catid.
					"\n AND section = 'com_nbeventmap_groupes'";
			$db->setQuery( $query );
			$list_id = $db->loadResultArray();
		}

		$needles = array(
			'categorie' => (int) $catid,
			'groupe' => $list_id,
		);

		
		$link = 'index.php?option=com_nbeventmap&view=nbeventmap&layout=categorie&catid='.$catid;
		if ($display  = 'list') $link = 'index.php?option=com_nbeventmap&view=nbeventmaplist&layout=categorie&catid='.$catid;

		if($item = NBEventMapHelperRoute::_findItem($needles)) {
			if(isset($item->query['layout'])) {
				$link .= '&layout='.$item->query['layout'];
			}
			$link .= '&Itemid='.$item->id;
		};

		return $link;
	}

	function _findItem($needles)
	{
		$component =& JComponentHelper::getComponent('com_nbeventmap');

		$menus	= &JApplication::getMenu('site', array());
		$items	= $menus->getItems('componentid', $component->id);

		$match = null;
		$match_all = null;

		foreach($needles as $needle => $id)
		{
			foreach($items as $item)
			{
				if ($needle == 'groupe') {
					foreach ($id as $id_group) {
						if (((@$item->query['view'] == 'nbeventmap') || (@$item->query['view'] == 'nbeventmaplist')) && (@$item->query['layout'] == $needle) && (@$item->query['catid'] == $id_group)) {
							$match = $item;
							break;
						}
					}
				} else {
					if (((@$item->query['view'] == 'nbeventmap') || (@$item->query['view'] == 'nbeventmaplist')) && (@$item->query['layout'] == $needle) && ((@$item->query['id'] == $id) || (@$item->query['catid'] == $id))) {
						$match = $item;
						break;
					}
				}
			}

			if(isset($match)) {
				break;
			}
			
			
			if ((@$item->query['view'] == 'nbeventmap') || (@$item->query['view'] == 'nbeventmaplist')) {
				$match_all = $item;
			}
		}

		if((!isset($match)) && (isset($match_all))) {
			$match = $match_all;
		}

		return $match;
	}
}
?>
